package com.ruoyi.web.controller.wxqy;

import com.ruoyi.common.annotation.ControllerAuthority;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.NumberUtils;
import com.ruoyi.common.utils.StrUtils;
import com.ruoyi.qywx.entity.ContactTag;
import com.ruoyi.qywx.entity.ContactTagGroup;
import com.ruoyi.qywx.model.vo.TagVo;
import com.ruoyi.qywx.service.inters.ContactTagServic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static com.ruoyi.common.core.domain.AjaxResult.error;

/**
 * @author : hcl
 * @Date : 2023/8/14 09:20
 */
@RestController
@ControllerAuthority
@RequestMapping("/wxqy/contact/tag")
public class ContactTagController extends BaseController {
    @Autowired
    private ContactTagServic contactTagServic;

    /**
     * @param type 0 自建 1 qywx
     */
    @PostMapping("/delete")
    public AjaxResult delete(@RequestParam Integer type, Long id) {
        if (!NumberUtils.isTrue(id)) {
            return error("请选择需要删除的标签");
        }
        return contactTagServic.deleteTag(type, id, getTeamId());
    }

    @PostMapping("/group/delete")
    public AjaxResult deleteGroup(@RequestParam Integer type, Long id) {
        if (!NumberUtils.isTrue(id)) {
            return error("请选择需要删除的分组");
        }
        return contactTagServic.deleteTagGroup(type, id, getTeamId());
    }

    /**
     * 添加新标签
     */
    @PostMapping("/add")
    public AjaxResult tagAdd(TagVo tag) {
        String validate = validate(tag.getType(), tag.getName());
        if (StrUtils.isNotEmpty(validate)) {
            return error(validate);
        }
        if (!NumberUtils.isTrue(tag.getGroupId())) {
            return error("请选择分组");
        }
        return contactTagServic.createTag(ContactTag.builder()
                .groupId(tag.getGroupId())
                .id(tag.getId())
                .name(tag.getName())
                .type(tag.getType())
                .teamId(getTeamId())
                .build());
    }

    /**
     * 添加/更新分组
     *
     * @param type 0 自建标签 1 企业微信标签
     */
    @PostMapping("/group/add")
    public AjaxResult groupAdd(Long groupId, Integer type, String name) {
        String validate = validate(type, name);
        if (StrUtils.isNotEmpty(validate)) {
            return error(validate);
        }
        return contactTagServic.createGroup(ContactTagGroup.builder()
                .id(groupId)
                .groupName(name)
                .type(type)
                .teamId(getTeamId())
                .build());
    }

    @GetMapping("/list")
    public AjaxResult tagList(Integer type) {
        if (type == null) {
            return error("请选择类型");
        }
        return success(contactTagServic.tagList(type, getTeamId()));
    }

    /**
     * 校验
     */
    private String validate(Integer type, String name) {
        if (type == null) {
            return "请选择类型";
        }
        if (StrUtils.isEmpty(name)) {
            return "名称不能为空";
        }
        return null;
    }
}
